package ca.uwo.android.directoryservice;


import android.content.Context;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import ca.uwo.android.BaseMenuActivity;
import ca.uwo.android.R;

public class ContactActivity extends BaseMenuActivity{
	
	private static final String TAG_NAME = "ContactActivity";
	
	// Custom Contact Dialog Constructor with given data
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.ds_dialog);
			
			DirectorySearchData data = (DirectorySearchData) getIntent().getExtras().getSerializable("DATA");

		TextView temp;
			//Name
			TextView nameView = (TextView)findViewById(R.id.dsName);
		nameView.setText(data.getFullName());
			
			//Email
			TextView emailValue = (TextView)findViewById(R.id.dsEmailValue);
			View emailView = (View)findViewById(R.id.dsEmail);
		String email = data.getEmail();
			if(email != null) {
			emailValue.setText(data.getEmail());
			Linkify.addLinks(emailValue, Linkify.EMAIL_ADDRESSES);
			emailView.setVisibility(View.VISIBLE);
			}
			else {
			emailView.setVisibility(View.GONE);
		}

//			Context context = getApplicationContext();
			//get inflater
			LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			if(data.isFaculty()) {
			// create Department Layout
				FacultyData.Roles roles[] = ((FacultyData)data).getRoles();
				if(roles != null) {
//					Log.i(TAG_NAME, "Got " + roles.length + " roles");
					LinearLayout departmentLayout = (LinearLayout)findViewById(R.id.contactFrame);
					for(int i = 0; i < roles.length;i++) {
					// create new department view
						View departmentView = inflater.inflate(R.layout.ds_department, null);
						
//						Log.i(TAG_NAME, "inflacted ds_department");

					// set department name
					String departmentName = roles[i].getDepartment();
						TextView departmentValue = (TextView)departmentView.findViewById(R.id.dsDeptValue);
					departmentValue.setText(departmentName);
						
						FacultyData.Roles.Contacts contacts[] = roles[i].getContacts();
						if(contacts != null) {
						// Get LinearLayout in department view
							LinearLayout contactLayout = (LinearLayout)departmentView.findViewById(R.id.dsContactList);
//							Log.i(TAG_NAME, "Got " + contacts.length + " contacts");
							for(int j = 0; j < contacts.length; j++) {
								View contactView = inflater.inflate(R.layout.ds_contact, null);
								View phoneView = (View)contactView.findViewById(R.id.dsContactPhone);
								View locationView = (View)contactView.findViewById(R.id.dsContactLocation);
							String phone = contacts[j].getPhoneNumber();
								if(phone != null) {
									TextView phoneValue = (TextView)contactView.findViewById(R.id.dsPhoneValue);
								phoneValue.setText(phone);
									
//									Log.i(TAG_NAME, "setting phone to " + phone);

								// Link Phone number
									Linkify.addLinks(phoneValue, PhoneUtil.PHONE, 
									PhoneUtil.CALLURI, PhoneUtil.PHONEMATCH, 
										PhoneUtil.PHONETRANSFORM);

								phoneView.setVisibility(View.VISIBLE);
								}
								else {
//									Log.i(TAG_NAME, "hiding phone number");
								phoneView.setVisibility(View.GONE);
							}

							String location = contacts[j].getLocation();
								if(location != null) {
//									Log.i(TAG_NAME, "Got location " + location);
									TextView locationValue = (TextView)contactView.findViewById(R.id.dsLocationValue);
								locationValue.setText(location);
								locationView.setVisibility(View.VISIBLE);
								}
								else {
//									
								locationView.setVisibility(View.GONE);
							}
							contactLayout.addView(contactView);
							}						
						}

						//Add Department
					departmentLayout.addView(departmentView);
				}
			}
		} else {
			LinearLayout departmentLayout = (LinearLayout) findViewById(R.id.contactFrame);
			View departmentView = inflater
					.inflate(R.layout.ds_department, null);

			// set program name
				String program = ((StudentData)data).getProgram();
				TextView departmentLabel = (TextView)departmentView.findViewById(R.id.dsDeptLabel);
			departmentLabel.setText("Program");

				TextView departmentValue = (TextView)departmentView.findViewById(R.id.dsDeptValue);
			departmentValue.setText(program);

			departmentLayout.addView(departmentView);
		}
	}

	

	
}
